import API from '@/api';

const IndexModel = {
  namespace: 'dict',

  state: {
    orgOptions: null,
    orgMap: null,
    planTypes: null,
    planOptions: null,
    recordTypes: null,
    recordOptions: null,
    orgList: null,
    msgData: {
      list: [],
      total: 0,
      current: 1,
      pageSize: 10,
      unRead: 0,
    },
  },

  effects: {
    *queryDict({ payload }, { select, call, put }) {
      const state = yield select((state) => state.dict.orgOptions);
      if (!state) {
        const res = yield call(API.getDictItemListByDictCode, payload);
        if (res?.t) {
          const orgMap = {};
          const orgOptions = res.t.map((x) => {
            orgMap[x.dictItemType] = x.dictItemName;
            return {
              label: x.dictItemName,
              value: x.dictItemType,
            };
          });
          yield put({
            type: 'saveDict',
            payload: { orgOptions, orgMap },
          });
        }
      }
    },
    *queryPlanType({ payload }, { select, call, put }) {
      const state = yield select((state) => state.dict.planOptions);
      if (!state) {
        const res = yield call(API.getYalx, payload);
        if (res?.t) {
          const planTypes = new Map();
          const planOptions = res.t.map((x) => {
            planTypes.set(x.dictItemType, x.dictItemName);
            return {
              label: x.dictItemName,
              value: x.dictItemType,
            };
          });
          yield put({
            type: 'saveDict',
            payload: { planOptions, planTypes },
          });
        }
      }
    },
    *queryRecordType({ payload }, { select, call, put }) {
      const state = yield select((state) => state.dict.recordOptions);
      if (!state) {
        const res = yield call(API.getYalx, payload);
        if (res?.t) {
          const recordTypes = new Map();
          const recordOptions = res.t.map((x) => {
            recordTypes.set(x.dictItemType, x.dictItemName);
            return {
              label: x.dictItemName,
              value: x.dictItemType,
            };
          });
          yield put({
            type: 'saveDict',
            payload: { recordOptions, recordTypes },
          });
        }
      }
    },
    *queryOrgList({ payload }, { call, put }) {
      const res = yield call(API.selectOrgListPage, payload);
      if (res?.t) {
        const data = res.t.map((x) => ({
          label: x.name,
          value: x.id,
        }));
        yield put({
          type: 'saveDict',
          payload: { orgList: data },
        });
      }
    },
    *queryNoticeList({ payload }, { call, put }) {
      const res = yield call(API.getMsgList, payload);
      if (res?.t?.list) {
        yield put({
          type: 'saveDict',
          payload: {
            msgData: {
              list: res.t.list.queryList,
              total: res.t.list.totalSize,
              current: res.t.list.currentPage,
              pageSize: res.t.list.pageSize,
              unRead: res.t.unread,
            },
          },
        });
      }
    },
  },
  reducers: {
    saveDict(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  // subscriptions: {
  //   setup({ dispatch, history }) {
  //     return history.listen(({ pathname }) => {
  //       if (pathname === '/notificationCenter') {
  //         dispatch({
  //           type: 'queryNoticeList',
  //           payload: {},
  //         });
  //       }
  //     });
  //   },
  // },
};

export default IndexModel;
